package tests;

import org.junit.internal.runners.statements.Fail;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import com.thoughtworks.selenium.SeleniumException;

public class LoginTest  {
	private static final String URL = "http://localhost:3000/";
	
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driverIE = new InternetExplorerDriver();
        testDriverLogin(driverIE);
        WebDriver driverFF = new FirefoxDriver();
        testDriverLogin(driverFF);
        /*
        WebDriver driverChr = new ChromeDriver();
        testDriverLogin(driverChr);
		*/
        System.out.println("All Tests Passed.");
    }
    
    private static void testDriverLogin(WebDriver driver) {
        // And now use this to visit Google
        driver.get(URL);
        
        if (!driver.getCurrentUrl().equals(URL)) {
        	throwException(driver, "Did not load correct page: " + driver.getCurrentUrl());
        }

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("login"));

        // Enter something to search for
        element.click();

        // Check the title of the page
        if (!driver.getCurrentUrl().equals(URL + "main.htm")) {
        	throwException(driver, "Could not login");
        }
        
        //Close the browser
        driver.quit();
    }
    
    private static void throwException(WebDriver driver, String message) {
    	driver.quit();
    	throw new SeleniumException(message);
    }
}